package com.lyp.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyp.lease.model.entity.*;
import com.lyp.lease.model.enums.ItemType;
import com.lyp.lease.web.admin.mapper.*;
import com.lyp.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyp.lease.web.admin.vo.attr.AttrValueVo;
import com.lyp.lease.web.admin.vo.graph.GraphVo;
import com.lyp.lease.web.admin.vo.room.RoomDetailVo;
import com.lyp.lease.web.admin.vo.room.RoomItemVo;
import com.lyp.lease.web.admin.vo.room.RoomQueryVo;
import com.lyp.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.rmi.dgc.Lease;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    PaymentTypeMapper paymentTypeMapper;
    @Autowired
    LeaseTermMapper leaseTermMapper;
    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    RoomFacilityMapper roomFacilityMapper;
    @Autowired
    RoomLabelMapper roomLabelMapper;
    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;




    @Transactional
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean isUpdate = roomSubmitVo.getId() != null ? true : false;  // 没必要
        boolean isUpdate2 = roomSubmitVo.getId() != null;

        super.saveOrUpdate(roomSubmitVo);
        // 如果是更新，先删除回显的数据，再插入数据
        if (isUpdate) {
            // 根据房间id删除当前房间所有图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            // 根据房间id删除当前房间所有属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            // 根据房间id删除当前房间所有配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            // 根据房间id删除当前房间所有标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            // 根据房间id删除当前房间所有支付
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            // 根据房间id删除当前房间所有可选
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

        }
        // 插入房间所有图片

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        // method1
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList2 = graphVoList.stream().map(graphVo -> {
                GraphInfo graphInfo = new GraphInfo();
                BeanUtils.copyProperties(graphVo, graphInfo); // 源只有url和name，而目标多出来的需要手动set
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                return graphInfo;
            }).toList();
            graphInfoService.saveBatch(graphInfoList2);
        }

        // method2
//        if (!graphVoList.isEmpty()) { // 插入图片的前提是有图片
//            List<GraphInfo> graphInfoList = new ArrayList<>();
//            for (GraphVo graphVo : graphVoList) {
//                GraphInfo graphInfo = new GraphInfo();
//                graphInfo.setItemType(ItemType.ROOM);
//                graphInfo.setItemId(roomSubmitVo.getId());
//                graphInfo.setName(graphVo.getName());
//                graphInfo.setUrl(graphVo.getUrl());
//                graphInfoList.add(graphInfo);
//            }
//            graphInfoService.saveBatch(graphInfoList);
//        }

        // 插入房间所有属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!attrValueIds.isEmpty()) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        // 插入房间所有配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!facilityInfoIds.isEmpty()) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomFacility roomFacility = new RoomFacility();
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacility.setFacilityId(attrValueId);
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        // 插入房间所有标签
        List<Long> labelInfoIdList = roomSubmitVo.getLabelInfoIds();
        if (!labelInfoIdList.isEmpty()) {
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomLabel roomLabel = new RoomLabel();
                roomLabel.setRoomId(roomSubmitVo.getId());
                roomLabel.setLabelId(attrValueId);
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        // 插入房间所有支付
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!paymentTypeIds.isEmpty()) {
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomPaymentType roomPaymentType = new RoomPaymentType();
                roomPaymentType.setRoomId(roomSubmitVo.getId());
                roomPaymentType.setPaymentTypeId(attrValueId);
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        // 插入房间所有可选
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            List<RoomLeaseTerm> roomLeaseTermList = leaseTermIds.stream().map(leaseTermId -> {
                RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                roomLeaseTerm.setRoomId(roomSubmitVo.getId());
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                return roomLeaseTerm;
            }).toList();
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

    }

    @Override
    public Page<RoomItemVo> pageItemRoom(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.selectPageItemRoom(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        // ----------------查询----------------
        // 1.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        // 2.查询当前房间的公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        // 3.查询当前房间的所有图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomInfo.getId());
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(graphInfoList)) {
            graphVoList = graphInfoList.stream().map(graphInfo -> {
                GraphVo graphVo = new GraphVo();
                graphVo.setName(graphInfo.getName());
                graphVo.setUrl(graphInfo.getUrl());
                return graphVo;
            }).toList();

        }
        // 4.根据房间id查询当前房间的所有属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectRoomAttrById(id);
        // 5.根据房间id查询当前房间的所有设施
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectRoomFacilityById(id);
        // 6.根据房间id查询房间的所有标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectRoomLabelById(id);
        // 7.根据id查询房间的支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectRoomPaymentTypeById(id);
        // 8.查询房间的租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectRoomLeaseTermById(id);
        // ----------------封装结果集----------------
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {

        // 删除房间图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);
        // 删除前房间的所有属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueMapper.delete(roomAttrValueLambdaQueryWrapper);

        // 设施
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityMapper.delete(roomFacilityLambdaQueryWrapper);
        // 标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelMapper.delete(roomLabelLambdaQueryWrapper);
        // 支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeMapper.delete(roomPaymentTypeLambdaQueryWrapper);
        // 房间的租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);



        // 删除房间
        roomInfoMapper.deleteById(id);

    }
}




